#ifndef VIRTUALDISK_H
#define VIRTUALDISK_H

#include <QString>
#include <QVector>
#include <QDateTime>

#define BLOCK_SIZE 1024
#define NUM_BLOCKS 8
#define FAT_SIZE NUM_BLOCKS * 2
#define EMPTY_BLOCK 0x0000
#define LAST_BLOCK 0xFFFF
#define FCB_SIZE 32
#define MAX_FCB_PER_BLOCK (BLOCK_SIZE / FCB_SIZE)

// 文件控制块结构
struct FCB {
    char name[9];       // 文件名(8字符+结束符)
    int size;           // 大小
    int first_block;    // 第一块块号
    char type;          // 1-文件, 2-目录, 0-已删除
    char datetime[15];  // 日期时间(yyyymmddhhmmss)

    FCB() {
        memset(name, 0, 9);
        size = 0;
        first_block = -1;
        type = 0;
        memset(datetime, 0, 15);
    }
};

class VirtualDisk {
private:
    QString filename;
    unsigned short fat[NUM_BLOCKS];
    char blocks[NUM_BLOCKS][BLOCK_SIZE];

    QString getCurrentTime();
    QVector<FCB> readFCBsFromBlock(int blockNum);
    void writeFCBsToBlock(int blockNum, const QVector<FCB>& fcbs);

public:
    VirtualDisk(QString fname);

    bool create();
    bool load();
    bool save();
    int findEmptyBlock();
    QVector<int> allocateBlocks(int count);
    void freeBlocks(int first_block);
    FCB findFCB(int dir_block, QString name);
    bool addFCB(int dir_block, FCB fcb);
    bool removeFCB(int dir_block, QString name);
    QVector<FCB> listFCBs(int dir_block);

    bool createDir(int parent_block, QString name);
    bool deleteDir(int parent_block, QString name);
    bool createFile(int parent_block, QString name, int size);
    bool deleteFile(int parent_block, QString name);
    QString getInfo();
};

#endif // VIRTUALDISK_H
